/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_ftp_detect.c                                     */
/*  NOTE      =                                                    */
/*  DATE      = 2016/04/05 by jikw                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include <ftp/ftp.h>
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "../../../src/module/ftp/config.h"
#include "apmtest_cunit.h"

extern int ftp_detect(Conntrack *p_st_conn, Apm_packet *p_st_packet);

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

static void case1(void)
{   
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    int i_ret = 0;
    
    i_ret = ftp_detect(p_st_conn, p_st_data_in);
    
    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    return;
}

static void case2(void)
{   
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    int i_ret = 0;
    
    //申请结构体内存
    p_st_data_in = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_data_in == NULL)
        goto ERROR;
    memset(p_st_data_in, 0x00, sizeof(Apm_packet));
    p_st_data_in->p_payload = frstrdup("5");
    p_st_data_in->src_ip = 30;
    p_st_data_in->src_port = 21;
    p_st_data_in->dst_ip = 40;
    p_st_data_in->dst_port = 15;
    
    i_ret = ftp_detect(p_st_conn, p_st_data_in);
    
    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    frfree(p_st_data_in->p_payload);
    frfree(p_st_data_in);
    
    return;

ERROR:
    if(p_st_data_in)
    {
        if(p_st_data_in->p_payload)
            frfree(p_st_data_in->p_payload);
        frfree(p_st_data_in);
    }
    
    return;
}

static void case3(void)
{   
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    int i_ret = 0;
    
    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->src_ip = 30;
    p_st_conn->src_port = 21;
    p_st_conn->dst_ip = 40;
    p_st_conn->dst_port = 15;
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;
    
    i_ret = ftp_detect(p_st_conn, p_st_data_in);
    
    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);
    CU_ASSERT_EQUAL(p_st_conn->flg_direct_known, FR_DIRECT_KNOWN);
    CU_ASSERT_EQUAL(p_st_conn->flg_chg_direct, FR_NO);

    frfree(p_st_conn);
    
    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    
    return;
}

static void case4(void)
{   
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    int i_ret = 0;
    
    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->src_ip = 40;
    p_st_conn->src_port = 15;
    p_st_conn->dst_ip = 30;
    p_st_conn->dst_port = 15;
    p_st_conn->flg_direct_known = FR_DIRECT_UNKNOWN;
    
    p_st_data_in = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_data_in == NULL)
        goto ERROR;
    memset(p_st_data_in, 0x00, sizeof(Apm_packet));
    p_st_data_in->p_payload = frstrdup(" ");
    p_st_data_in->src_ip = 30;
    p_st_data_in->src_port = 15;
    p_st_data_in->dst_ip = 40;
    p_st_data_in->dst_port = 15;
    
    i_ret = ftp_detect(p_st_conn, p_st_data_in);
    
    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);
    CU_ASSERT_EQUAL(p_st_conn->flg_direct_known, FR_DIRECT_UNKNOWN);
    CU_ASSERT_EQUAL(p_st_conn->flg_chg_direct, FR_NO);
    CU_ASSERT_EQUAL(p_st_conn->src_ip, 40);
    CU_ASSERT_EQUAL(p_st_conn->src_port, 15);
    CU_ASSERT_EQUAL(p_st_conn->dst_ip, 30);
    CU_ASSERT_EQUAL(p_st_conn->dst_port, 15);

    frfree(p_st_conn);
    frfree(p_st_data_in->p_payload);
    frfree(p_st_data_in);
    
    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_data_in)
    {
        if(p_st_data_in->p_payload)
            frfree(p_st_data_in->p_payload);
        frfree(p_st_data_in);
    }    
    
    return;
}

static void case5(void)
{   
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    int i_ret = 0;
    
    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->src_ip = 30;
    p_st_conn->src_port = 21;
    p_st_conn->dst_ip = 40;
    p_st_conn->dst_port = 15;
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;
    
    p_st_data_in = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_data_in == NULL)
        goto ERROR;
    memset(p_st_data_in, 0x00, sizeof(Apm_packet));
    p_st_data_in->p_payload = frstrdup("USER test01");
    p_st_data_in->src_ip = 40;
    p_st_data_in->src_port = 21;
    p_st_data_in->dst_ip = 30;
    p_st_data_in->dst_port = 15;
    
    i_ret = ftp_detect(p_st_conn, p_st_data_in);
    
    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_YES);
    CU_ASSERT_EQUAL(p_st_conn->flg_direct_known, FR_DIRECT_KNOWN);
    CU_ASSERT_EQUAL(p_st_conn->flg_chg_direct, FR_NO);
    CU_ASSERT_EQUAL(p_st_conn->src_ip, 30);
    CU_ASSERT_EQUAL(p_st_conn->src_port, 21);
    CU_ASSERT_EQUAL(p_st_conn->dst_ip, 40);
    CU_ASSERT_EQUAL(p_st_conn->dst_port, 15);

    frfree(p_st_conn);
    frfree(p_st_data_in->p_payload);
    frfree(p_st_data_in);
     
    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_data_in)
    {
        if(p_st_data_in->p_payload)
            frfree(p_st_data_in->p_payload);
        frfree(p_st_data_in);
    }    
    
    return;
}

static void case6(void)
{   
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    int i_ret = 0;
    
    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->src_ip = 30;
    p_st_conn->src_port = 21;
    p_st_conn->dst_ip = 40;
    p_st_conn->dst_port = 15;
    p_st_conn->flg_direct_known = FR_DIRECT_UNKNOWN;
    
    p_st_data_in = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_data_in == NULL)
        goto ERROR;
    memset(p_st_data_in, 0x00, sizeof(Apm_packet));
    p_st_data_in->p_payload = frstrdup("5");
    p_st_data_in->src_ip = 40;
    p_st_data_in->src_port = 15;
    p_st_data_in->dst_ip = 30;
    p_st_data_in->dst_port = 21;
    
    i_ret = ftp_detect(p_st_conn, p_st_data_in);
    
    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_YES);
    CU_ASSERT_EQUAL(p_st_conn->flg_direct_known, FR_DIRECT_KNOWN);
    CU_ASSERT_EQUAL(p_st_conn->flg_chg_direct, FR_NO);
    CU_ASSERT_EQUAL(p_st_conn->src_ip, 30);
    CU_ASSERT_EQUAL(p_st_conn->src_port, 21);
    CU_ASSERT_EQUAL(p_st_conn->dst_ip, 40);
    CU_ASSERT_EQUAL(p_st_conn->dst_port, 15);

    frfree(p_st_conn);
    frfree(p_st_data_in->p_payload);
    frfree(p_st_data_in);
 
    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_data_in)
    {
        if(p_st_data_in->p_payload)
            frfree(p_st_data_in->p_payload);
        frfree(p_st_data_in);
    }    
    
    return;
}

static void case7(void)
{   
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    int i_ret = 0;
    
    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->src_ip = 40;
    p_st_conn->src_port = 21;
    p_st_conn->dst_ip = 30;
    p_st_conn->dst_port = 15;
    p_st_conn->flg_direct_known = FR_DIRECT_UNKNOWN;
    
    p_st_data_in = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_data_in == NULL)
        goto ERROR;
    memset(p_st_data_in, 0x00, sizeof(Apm_packet));
    p_st_data_in->p_payload = frstrdup("USER test01");
    p_st_data_in->src_ip = 40;
    p_st_data_in->src_port = 21;
    p_st_data_in->dst_ip = 30;
    p_st_data_in->dst_port = 15;
    
    i_ret = ftp_detect(p_st_conn, p_st_data_in);
    
    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_YES);
    CU_ASSERT_EQUAL(p_st_conn->flg_direct_known, FR_DIRECT_KNOWN);
    CU_ASSERT_EQUAL(p_st_conn->flg_chg_direct, FR_NO);
    CU_ASSERT_EQUAL(p_st_conn->src_ip, 40);
    CU_ASSERT_EQUAL(p_st_conn->src_port, 21);
    CU_ASSERT_EQUAL(p_st_conn->dst_ip, 30);
    CU_ASSERT_EQUAL(p_st_conn->dst_port, 15);

    frfree(p_st_conn);
    frfree(p_st_data_in->p_payload);
    frfree(p_st_data_in);
    
    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_data_in)
    {
        if(p_st_data_in->p_payload)
            frfree(p_st_data_in->p_payload);
        frfree(p_st_data_in);
    }    
    
    return;
}

static void case8(void)
{   
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    int i_ret = 0;
    
    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->src_ip = 40;
    p_st_conn->src_port = 21;
    p_st_conn->dst_ip = 30;
    p_st_conn->dst_port = 15;
    p_st_conn->flg_direct_known = FR_DIRECT_UNKNOWN;
    
    p_st_data_in = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_data_in == NULL)
        goto ERROR;
    memset(p_st_data_in, 0x00, sizeof(Apm_packet));
    p_st_data_in->p_payload = frstrdup(" ");
    p_st_data_in->src_ip = 30;
    p_st_data_in->src_port = 15;
    p_st_data_in->dst_ip = 40;
    p_st_data_in->dst_port = 21;
    
    i_ret = ftp_detect(p_st_conn, p_st_data_in);
    
    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_YES);
    CU_ASSERT_EQUAL(p_st_conn->flg_direct_known, FR_DIRECT_KNOWN);
    CU_ASSERT_EQUAL(p_st_conn->flg_chg_direct, FR_NO);
    CU_ASSERT_EQUAL(p_st_conn->src_ip, 40);
    CU_ASSERT_EQUAL(p_st_conn->src_port, 21);
    CU_ASSERT_EQUAL(p_st_conn->dst_ip, 30);
    CU_ASSERT_EQUAL(p_st_conn->dst_port, 15);

    frfree(p_st_conn);
    frfree(p_st_data_in->p_payload);
    frfree(p_st_data_in);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_data_in)
    {
        if(p_st_data_in->p_payload)
            frfree(p_st_data_in->p_payload);
        frfree(p_st_data_in);
    }    
    
    return;
}

static void case9(void)
{   
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    int i_ret = 0;
    
    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->src_ip = 40;
    p_st_conn->src_port = 15;
    p_st_conn->dst_ip = 30;
    p_st_conn->dst_port = 21;
    p_st_conn->flg_direct_known = FR_DIRECT_UNKNOWN;
    
    p_st_data_in = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_data_in == NULL)
        goto ERROR;
    memset(p_st_data_in, 0x00, sizeof(Apm_packet));
    p_st_data_in->p_payload = frstrdup("USER test01");
    p_st_data_in->src_ip = 30;
    p_st_data_in->src_port = 21;
    p_st_data_in->dst_ip = 40;
    p_st_data_in->dst_port = 15;
    
    i_ret = ftp_detect(p_st_conn, p_st_data_in);
    
    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_YES);
    CU_ASSERT_EQUAL(p_st_conn->flg_direct_known, FR_DIRECT_KNOWN);
    CU_ASSERT_EQUAL(p_st_conn->flg_chg_direct, FR_YES);
    CU_ASSERT_EQUAL(p_st_conn->src_ip, 30);
    CU_ASSERT_EQUAL(p_st_conn->src_port, 21);
    CU_ASSERT_EQUAL(p_st_conn->dst_ip, 40);
    CU_ASSERT_EQUAL(p_st_conn->dst_port, 15);

    frfree(p_st_conn);
    frfree(p_st_data_in->p_payload);
    frfree(p_st_data_in);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_data_in)
    {
        if(p_st_data_in->p_payload)
            frfree(p_st_data_in->p_payload);
        frfree(p_st_data_in);
    }    
    
    return;
}

int add_suite_ftp_detect(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" ftp_detect()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL ||
        CU_add_test(pSuite, "case4", case4) == NULL ||
        CU_add_test(pSuite, "case5", case5) == NULL ||
        CU_add_test(pSuite, "case6", case6) == NULL ||
        CU_add_test(pSuite, "case7", case7) == NULL ||
        CU_add_test(pSuite, "case8", case8) == NULL ||
        CU_add_test(pSuite, "case9", case9) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
